clear *

use "${root}/data/processed/final_sample.dta", clear

drop if irregular == 1

merge 1:1 mun_code year using "${root}/data/processed/demographic_dataset.dta"


* gen left mayor dummy
gen left_mayor=.
replace left_mayor=1 if margin_mayor_left>=0 & margin_mayor_left!=.
replace left_mayor=0 if margin_mayor_left<0 & margin_mayor_left!=.

* more manageable name for labor force participation rate and education variables
rename econ_active_workforce lfp
gen high_educ=bachelor_perc
gen low_educ=illiteracy_perc 

* gen log of population
gen ln_pop = ln(mun_pop)*100

* gen log of BF
gen ln_bf = ln(monthly_avg_households_pc_bf)*100
gen ln_bf_value = ln(total_annual_value_pc_bf)*100

*gen log of average earnings
label var avg_earnings "Average earnings (2016 constant values)"
label var median_earnings "Median earnings (2016 constant values)"
gen ln_earnings=ln(median_earnings)*100

*gen amendments as share of total revenues
gen tot_rev_sum2y = tot_rev_cp_t_0 + tot_rev_cp_t_minus1
gen amend_exec_sum2y = amendment_executed_cp_t_0 + amendment_executed_cp_t_minus1
gen amend_auth_sum2y = amendment_authorized_cp_t_0 + amendment_authorized_cp_t_minus1

gen amend_exec_share2y = amend_exec_sum2y/tot_rev_sum2y*100
gen amend_auth_share2y = amend_auth_sum2y/tot_rev_sum2y*100

local covariates ln_pop urb_perc white_perc ln_earnings high_educ low_educ lfp ln_bf ln_bf_value amend_exec_share2y amend_auth_share2y north northeast southeast south midwest
local geo_covariates north northeast southeast south midwest
local nongeo_covariates ln_pop urb_perc white_perc ln_earnings high_educ low_educ lfp ln_bf ln_bf_value amend_exec_share2y amend_auth_share2y

* filter out FE
foreach covariate of local nongeo_covariates {
	qui areg `covariate' yy*, absorb(mun_code)
	qui predict res_`covariate', res
	}

foreach covariate of local geo_covariates {
	gen res_`covariate'=`covariate'
	}
	

* using our preferred RD specification

* Keep only the baseline final sample
keep if baseline_sample == 1

foreach covariate of local geo_covariates {
	rdrobust `covariate' margin_mayor_left, vce(cluster mun_code) all
	local `covariate'_rd : di %9.2f (e(tau_bc))
	local se_`covariate'_rd : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_rd : di %9.2f (e(pv_rb))	
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_rd
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_rd "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_rd "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_rd "$^{***}$"
		}
	
	}
local n_rd = (e(N))
local eff_rd= e(N_h_l) + e(N_h_r)
	
foreach covariate of local nongeo_covariates {
	rdrobust res_`covariate' margin_mayor_left, vce(cluster mun_code) all
	local `covariate'_rd : di %9.2f (e(tau_bc))
	local se_`covariate'_rd : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_rd : di %9.2f (e(pv_rb))
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_rd
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_rd "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_rd "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_rd "$^{***}$"
		}
	
	}

* Lame-duck subsample

foreach covariate of local geo_covariates {
	rdrobust `covariate' margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all
	local `covariate'_lame : di %9.2f (e(tau_bc))
	local se_`covariate'_lame : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_lame : di %9.2f (e(pv_rb))	
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_lame
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_lame "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_lame "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_lame "$^{***}$"
		}
	
	}
local n_lame = (e(N))
local eff_lame= e(N_h_l) + e(N_h_r)
	
foreach covariate of local nongeo_covariates {
	rdrobust res_`covariate' margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all
	local `covariate'_lame : di %9.2f (e(tau_bc))
	local se_`covariate'_lame : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_lame : di %9.2f (e(pv_rb))
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_lame
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_lame "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_lame "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_lame "$^{***}$"
		}
	
	}


* Tiebout subsample

foreach covariate of local geo_covariates {
	rdrobust `covariate' margin_mayor_left if tiebout_median_sample == 1, vce(cluster mun_code) all
	local `covariate'_tiebout : di %9.2f (e(tau_bc))
	local se_`covariate'_tiebout : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_tiebout : di %9.2f (e(pv_rb))	
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_tiebout
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_tiebout "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_tiebout "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_tiebout "$^{***}$"
		}
	
	}
local n_tiebout = (e(N))
local eff_tiebout= e(N_h_l) + e(N_h_r)
	
foreach covariate of local nongeo_covariates {
	rdrobust res_`covariate' margin_mayor_left if tiebout_median_sample == 1, vce(cluster mun_code) all
	local `covariate'_tiebout : di %9.2f (e(tau_bc))
	local se_`covariate'_tiebout : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_tiebout : di %9.2f (e(pv_rb))
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_tiebout
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_tiebout "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_tiebout "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_tiebout "$^{***}$"
		}
	
	}


* Coalition distance subsample

foreach covariate of local geo_covariates {
	rdrobust `covariate' margin_mayor_left if coal_dist_median_sample == 1, vce(cluster mun_code) all
	local `covariate'_coal : di %9.2f (e(tau_bc))
	local se_`covariate'_coal : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_coal : di %9.2f (e(pv_rb))	
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_coal
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_coal "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_coal "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_coal "$^{***}$"
		}
	
	}
local n_coal = (e(N))
local eff_coal= e(N_h_l) + e(N_h_r)
	
foreach covariate of local nongeo_covariates {
	rdrobust res_`covariate' margin_mayor_left if coal_dist_median_sample == 1, vce(cluster mun_code) all
	local `covariate'_coal : di %9.2f (e(tau_bc))
	local se_`covariate'_coal : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_coal : di %9.2f (e(pv_rb))
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_coal
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_coal "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_coal "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_coal "$^{***}$"
		}
	
	}



* Oil windfall subsample

foreach covariate of local geo_covariates {
	rdrobust `covariate' margin_mayor_left if oil_sample == 1, vce(cluster mun_code) all
	local `covariate'_oil : di %9.2f (e(tau_bc))
	local se_`covariate'_oil : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_oil : di %9.2f (e(pv_rb))	
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_oil
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_oil "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_oil "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_oil "$^{***}$"
		}
	
	}
local n_oil = (e(N))
local eff_oil= e(N_h_l) + e(N_h_r)
	
foreach covariate of local nongeo_covariates {
	rdrobust res_`covariate' margin_mayor_left if oil_sample == 1, vce(cluster mun_code) all
	local `covariate'_oil : di %9.2f (e(tau_bc))
	local se_`covariate'_oil : di %9.2f (e(se_tau_rb))
	local pv_`covariate'_oil : di %9.2f (e(pv_rb))
	
	if e(pv_rb)>0.10 {
		* local stars_`covariate'_oil
	}        

	if e(pv_rb)<=0.10 {
		* local stars_`covariate'_oil "$^{*}$"
	}    

	if e(pv_rb)<=0.05 {
		* local stars_`covariate'_oil "$^{**}$"
	}    

	if e(pv_rb)<=0.01 {
		* local stars_`covariate'_oil "$^{***}$"
		}
	
	}


* write table
texdoc init "${root}/results/tables/covariate_balance_table_subsample.tex", replace force

tex \caption{Difference in municipality characteristics between left and non-left mayors at the threshold}

tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule

tex 													&        Baseline 				& \multicolumn{4}{c}{Subsamples} 								\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 													& 								& 	Lame Duck 			& Tiebout $<$ median	 								& Ideology distance $>$ median  						& Oil windfall					\\
tex \midrule

tex \multicolumn{6}{l}{Labor market and demographic covariates} \\
tex \midrule
tex log(Median earnings) $\times 100$       & `ln_earnings_rd'`stars_ln_earnings_rd' 	& `ln_earnings_lame'`stars_ln_earnings_lame' & `ln_earnings_tiebout'`stars_ln_earnings_tiebout' 	& `ln_earnings_coal'`stars_ln_earnings_coal' 	& `ln_earnings_oil'`stars_ln_earnings_oil'    \\
tex 				            			& (`se_ln_earnings_rd') 					& (`se_ln_earnings_lame') 				 & (`se_ln_earnings_tiebout') 					& (`se_ln_earnings_coal')  				& (`se_ln_earnings_oil') 					\\
tex Labor force participation          		& `lfp_rd'`stars_lfp_rd' 					& `lfp_lame'`stars_lfp_lame'  				 & `lfp_tiebout'`stars_lfp_tiebout' 					& `lfp_coal'`stars_lfp_coal' 					& `lfp_oil'`stars_lfp_oil'      				\\
tex  				            			& (`se_lfp_rd') 							& (`se_lfp_lame') 						 & (`se_lfp_tiebout') 							& (`se_lfp_coal')  						& (`se_lfp_oil') 							\\
tex log(Population) $\times 100$         	& `ln_pop_rd'`stars_ln_pop_rd'			& `ln_pop_lame'`stars_ln_pop_lame' 			 & `ln_pop_tiebout'`stars_ln_pop_tiebout' 			& `ln_pop_coal'`stars_ln_pop_coal' 			& `ln_pop_oil'`stars_ln_pop_oil'      		\\
tex                     					& (`se_ln_pop_rd') 						& (`se_ln_pop_lame') 						 & (`se_ln_pop_tiebout') 						& (`se_ln_pop_coal')  						& (`se_ln_pop_oil') 							\\
tex \% Urban          						& `urb_perc_rd'`stars_urb_perc_rd'		& `urb_perc_lame'`stars_urb_perc_lame' 		 & `urb_perc_tiebout'`stars_urb_perc_tiebout' 		& `urb_perc_coal'`stars_urb_perc_coal' 		& `urb_perc_oil'`stars_urb_perc_oil'      	\\
tex                     					& (`se_urb_perc_rd') 						& (`se_urb_perc_lame') 					 & (`se_urb_perc_tiebout') 						& (`se_urb_perc_coal')  					& (`se_urb_perc_oil') 						\\
tex \% White          						& `white_perc_rd'`stars_white_perc_rd' 	& `white_perc_lame'`stars_white_perc_lame' 	 & `white_perc_tiebout'`stars_white_perc_tiebout' 	& `white_perc_coal'`stars_white_perc_coal' 	& `white_perc_oil'`stars_white_perc_oil'    	\\
tex                     					& (`se_white_perc_rd') 					& (`se_white_perc_lame') 					 & (`se_white_perc_tiebout') 					& (`se_white_perc_coal')  					& (`se_white_perc_oil') 						\\
tex \% Higher education        				& `high_educ_rd'`stars_high_educ_rd' 		& `high_educ_lame'`stars_high_educ_lame'  	 & `high_educ_tiebout'`stars_high_educ_tiebout' 		& `high_educ_coal'`stars_high_educ_coal' 		& `high_educ_oil'`stars_high_educ_oil'      	\\
tex 				            			& (`se_high_educ_rd') 						& (`se_high_educ_lame') 					 & (`se_high_educ_tiebout') 						& (`se_high_educ_coal')  					& (`se_high_educ_oil') 						\\
tex \% Illiterate        					& `low_educ_rd'`stars_low_educ_rd' 		& `low_educ_lame'`stars_low_educ_lame'  	 & `low_educ_tiebout'`stars_low_educ_tiebout' 		& `low_educ_coal'`stars_low_educ_coal' 		& `low_educ_oil'`stars_low_educ_oil'      	\\
tex 				           				& (`se_low_educ_rd') 						& (`se_low_educ_lame') 					 & (`se_low_educ_tiebout') 						& (`se_low_educ_coal')  					& (`se_low_educ_oil') 						\\

tex \midrule
tex \multicolumn{6}{l}{Geographic indicators} \\
tex \midrule
tex North 			         				& `north_rd'`stars_north_rd'  			& `north_lame'`stars_north_lame'  			 & `north_tiebout'`stars_north_tiebout' 				& `north_coal'`stars_north_coal' 				& `north_oil'`stars_north_oil'      		\\
tex 			                			& (`se_north_rd') 							& (`se_north_lame') 						 & (`se_north_tiebout') 							& (`se_north_coal')  						& (`se_north_oil') 						\\
tex Northeast 			         			& `northeast_rd'`stars_northeast_rd'  	& `northeast_lame'`stars_northeast_lame' 	 & `northeast_tiebout'`stars_northeast_tiebout' 		& `northeast_coal'`stars_northeast_coal' 		& `northeast_oil'`stars_northeast_oil'    \\
tex 			                			& (`se_northeast_rd') 						& (`se_northeast_lame') 					 & (`se_northeast_tiebout') 						& (`se_northeast_coal')  					& (`se_northeast_oil') 					\\
tex South 			         				& `south_rd'`stars_south_rd'  			& `south_lame'`stars_south_lame' 			 & `south_tiebout'`stars_south_tiebout' 				& `south_coal'`stars_south_coal' 				& `south_oil'`stars_south_oil'      		\\
tex 			                			& (`se_south_rd') 							& (`se_south_lame') 						 & (`se_south_tiebout') 							& (`se_south_coal')  						& (`se_south_oil') 						\\
tex Southeast 			     				& `southeast_rd'`stars_southeast_rd' 		& `southeast_lame'`stars_southeast_lame' 	 & `southeast_tiebout'`stars_southeast_tiebout' 		& `southeast_coal'`stars_southeast_coal' 		& `southeast_oil'`stars_southeast_oil'    \\
tex 			                			& (`se_southeast_rd') 						& (`se_southeast_lame') 					 & (`se_southeast_tiebout') 						& (`se_southeast_coal')  					& (`se_southeast_oil') 					\\	
tex Midwest 			      				& `midwest_rd'`stars_midwest_rd'			& `midwest_lame'`stars_midwest_lame' 		 & `midwest_tiebout'`stars_midwest_tiebout' 			& `midwest_coal'`stars_midwest_coal' 			& `midwest_oil'`stars_midwest_oil'      	\\
tex 										& (`se_midwest_rd') 						& (`se_midwest_lame') 					 & (`se_midwest_tiebout') 						& (`se_midwest_coal')  					& (`se_midwest_oil') 					\\	

tex \midrule
tex \multicolumn{6}{l}{Other covariates} \\
tex \midrule

tex log(Bolsa Familia households) $\times 100$       	& `ln_bf_rd'`stars_ln_bf_rd' 	& `ln_bf_lame'`stars_ln_bf_lame'  			 & `ln_bf_tiebout'`stars_ln_bf_tiebout' 				& `ln_bf_coal'`stars_ln_bf_coal' 				& `ln_bf_oil'`stars_ln_bf_oil'      			\\
tex 			               				& (`se_ln_bf_rd') 							& (`se_ln_bf_lame') 						 & (`se_ln_bf_tiebout') 							& (`se_ln_bf_coal')  						& (`se_ln_bf_oil') 							\\

tex log(Bolsa Familia receipts) $\times 100$ & `ln_bf_value_rd'`stars_ln_bf_value_rd' & `ln_bf_value_lame'`stars_ln_bf_value_lame' & `ln_bf_value_tiebout'`stars_ln_bf_value_tiebout' 	& `ln_bf_value_coal'`stars_ln_bf_value_coal' 	& `ln_bf_value_oil'`stars_ln_bf_value_oil'    \\
tex 					        			& (`se_ln_bf_value_rd') 					& (`se_ln_bf_value_lame') 				 & (`se_ln_bf_value_tiebout') 					& (`se_ln_bf_value_coal')  				& (`se_ln_bf_value_oil') 					\\

tex Authorized amendments       			& `amend_auth_share2y_rd' 					& `amend_auth_share2y_lame'  				 & `amend_auth_share2y_tiebout' 					& `amend_auth_share2y_coal' 				& `amend_auth_share2y_oil'      				\\
tex 			               				& (`se_amend_auth_share2y_rd') 			& (`se_amend_auth_share2y_lame') 			 & (`se_amend_auth_share2y_tiebout') 			& (`se_amend_auth_share2y_coal') 			& (`se_amend_auth_share2y_oil') 				\\

tex Executed amendments				      	& `amend_exec_share2y_rd' 					& `amend_exec_share2y_lame'  				 & `amend_exec_share2y_tiebout' 					& `amend_exec_share2y_coal'				& `amend_exec_share2y_oil'					\\
tex 			               				& (`se_amend_exec_share2y_rd') 			& (`se_amend_exec_share2y_lame') 			& (`se_amend_exec_share2y_tiebout') 				& (`se_amend_exec_share2y_coal')  			& (`se_amend_exec_share2y_oil') 				\\
	
tex \bottomrule
tex Observations (all)     					& `n_rd' 									& `n_lame' 								 & `n_tiebout' 									& `n_coal' 								& `n_oil'      								\\
tex Observations (effective)				& `eff_rd'									& `eff_lame' 								 & `eff_tiebout' 									& `eff_coal' 								& `eff_oil' 									\\
tex \bottomrule
tex \end{tabularx}}
	 
	 
texdoc close
